#include "Beam_Quality_LaserWavelngth_View.h"
#include <QFormLayout>
#include <QGroupBox>
#include <QFrame>

Beam_Quality_Laser_Wavelength_View::Beam_Quality_Laser_Wavelength_View(QWidget *parent)
    : QDialog(parent)
{
    setupUi();
    connectSignals();

    // 设置窗口属性
    setWindowTitle(tr("Check Laser Wavelength"));
    setMinimumWidth(350);
    setFixedHeight(sizeHint().height());
    setModal(true);
}

Beam_Quality_Laser_Wavelength_View::~Beam_Quality_Laser_Wavelength_View()
{

}
void Beam_Quality_Laser_Wavelength_View::setupUi()
{
    // 创建主布局 - 保持垂直布局
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setSpacing(15);
    mainLayout->setContentsMargins(20, 20, 20, 20);

    // 创建标题标签
    m_titleLabel = new QLabel(tr("波长和焦距控制"), this);
    QFont titleFont = m_titleLabel->font();
    titleFont.setBold(true);
    titleFont.setPointSize(titleFont.pointSize() + 1);
    m_titleLabel->setFont(titleFont);
    m_titleLabel->setAlignment(Qt::AlignCenter);
    mainLayout->addWidget(m_titleLabel);

    // 创建分隔线
    QFrame *line = new QFrame(this);
    line->setFrameShape(QFrame::HLine);
    line->setFrameShadow(QFrame::Sunken);
    mainLayout->addWidget(line);

    // 创建输入区域组框
    QGroupBox *inputGroup = new QGroupBox(this);
    QVBoxLayout *groupLayout = new QVBoxLayout(inputGroup);
    groupLayout->setContentsMargins(15, 15, 15, 15);

    // 波长输入
    QHBoxLayout *wavelengthLayout = new QHBoxLayout();
    wavelengthLayout->addWidget(new QLabel(tr("波长:")));

    m_wavelengthSpinBox = new QDoubleSpinBox(this);
    m_wavelengthSpinBox->setRange(1.0, 10000.0);
    m_wavelengthSpinBox->setDecimals(3);
    m_wavelengthSpinBox->setValue(1550.0);
    m_wavelengthSpinBox->setSingleStep(0.01);
    wavelengthLayout->addWidget(m_wavelengthSpinBox);

    m_unitLabel = new QLabel("nm", this);
    wavelengthLayout->addWidget(m_unitLabel);
    wavelengthLayout->addStretch();

    groupLayout->addLayout(wavelengthLayout);

    // 新增焦距输入
    QHBoxLayout *focalLayout = new QHBoxLayout();
    focalLayout->addWidget(new QLabel(tr("焦距:")));

    m_focallengthSpinBox = new QDoubleSpinBox(this);
    m_focallengthSpinBox->setRange(1.0, 1000.0); // 设置合理范围
    m_focallengthSpinBox->setDecimals(2);
    m_focallengthSpinBox->setValue(100.0); // 默认值
    m_focallengthSpinBox->setSingleStep(1.0);
    focalLayout->addWidget(m_focallengthSpinBox);

    QLabel *focalUnit = new QLabel("mm", this);
    focalLayout->addWidget(focalUnit);
    focalLayout->addStretch();

    groupLayout->addLayout(focalLayout);

    // 将组框添加到主布局
    mainLayout->addWidget(inputGroup);

    QHBoxLayout *buttonLayout = new QHBoxLayout();
    buttonLayout->setSpacing(10); // 设置按钮间距

    // 创建确定和取消按钮
    m_okButton = new QPushButton(tr("OK"), this);
    m_cancelButton = new QPushButton(tr("Cancel"), this);

    // 设置按钮样式
    m_okButton->setMinimumWidth(120);
    m_cancelButton->setMinimumWidth(120);
    m_okButton->setDefault(true);

    // 将按钮添加到布局
    buttonLayout->addWidget(m_okButton);
    buttonLayout->addWidget(m_cancelButton);

    // 将按钮布局添加到主布局
    mainLayout->addLayout(buttonLayout);
}
void Beam_Quality_Laser_Wavelength_View::connectSignals()
{
    // 连接按钮信号
    connect(m_okButton, &QPushButton::clicked, this, &QDialog::accept);
    connect(m_cancelButton, &QPushButton::clicked, this, &QDialog::reject);
    connect(m_cancelButton, &QPushButton::clicked, this, &Beam_Quality_Laser_Wavelength_View::DialogAccepted);
    connect(m_okButton, &QPushButton::clicked, this, &Beam_Quality_Laser_Wavelength_View::DialogAccepted);

    // 设置回车键触发确定按钮
    m_okButton->setAutoDefault(true);
    m_okButton->setDefault(true);
}

double Beam_Quality_Laser_Wavelength_View::getWavelength() const
{
    return m_wavelengthSpinBox->value();
}


double Beam_Quality_Laser_Wavelength_View::getFocallength() const
{
    return m_focallengthSpinBox->value();
}

void Beam_Quality_Laser_Wavelength_View::setWavelength(double wavelength)
{
    m_wavelengthSpinBox->setValue(wavelength);
}

void Beam_Quality_Laser_Wavelength_View::setFocallength(double focallength)
{
    m_focallengthSpinBox->setValue(focallength);
}

void Beam_Quality_Laser_Wavelength_View::DialogAccepted()
{
    emit sendWavelength(m_wavelengthSpinBox->value(),m_focallengthSpinBox->value());
}